import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:四数之和
 * User: bjc
 * Date: 2025-11-06
 * Time: 20:52
 */
public class Test08_fourSum {
    public static List<List<Integer>> fourSum1(int[] nums, int target){
        //1.先对数组进行排序
        Arrays.sort(nums);
        //2.使用三数之和解决
        List<List<Integer>> result = new ArrayList<>();
        for (int i = 0; i < nums.length - 3; i++) {
            for (int j = i+1; j < nums.length - 2; j++) {
                int left = j+1,right = nums.length-1;
                while(left < right){
                    long sum = (long) nums[i]+nums[j]+nums[left]+nums[right];
                    if(sum > target) right--;
                    else if(sum < target) left++;
                    else {
                        result.add(Arrays.asList(nums[i],nums[j],nums[left],nums[right]));
                        left++;
                        right--;
                        //去重
                        while(left < right && nums[left] == nums[left-1]) left++;
                        while(left < right && nums[right] == nums[right+1]) right--;
                    }
                }
                //去重
                while (j < nums.length - 1 && nums[j] == nums[j+1]) j++;
            }
            //去重
            while (i < nums.length - 1 && nums[i] == nums[i+1]) i++;
        }
        return result;
    }
    public static List<List<Integer>> fourSum(int[] nums, int target) {
        //1.先对数组进行排序
        Arrays.sort(nums);
        //2.使用三数之和解决
        List<List<Integer>> result = new ArrayList<>();
        for(int i = 0;i < nums.length - 3;i++){
            long tag = (long) target - nums[i];
            List<List<Integer>> lists = threeSum(nums, tag,i+1);
            for (List<Integer> list : lists) {
                List<Integer> newList = new ArrayList<>(list);
                newList.add(nums[i]);
                result.add(newList);
            }
            while(i < nums.length - 2 && nums[i] == nums[i+1]) i++;
        }
        return result;

    }
    public static List<List<Integer>> threeSum(int[] nums,long target,int start){
        List<List<Integer>> result = new ArrayList<>();
        for (int i = start;i < nums.length - 2;i++){
            int left = i + 1,right = nums.length - 1;
            while (left < right){
                long sum = (long) nums[left] + nums[right] + nums[i];
                if(sum > target) right--;
                else if(sum < target) left++;
                else {
                    result.add(Arrays.asList(nums[i],nums[left],nums[right]));
                    left++;
                    right--;
                    while(left < right && nums[left] == nums[left-1]) left++;
                    while(left < right && nums[right] == nums[right+1]) right--;
                }
            }
            while(i < nums.length - 1 && nums[i] == nums[i+1]) i++;
        }
        return result;
    }
    public static void main(String[] args) {
        int[] nums = {1,0,-1,0,-2,2};
        int target = 0;
        List<List<Integer>> lists = fourSum1(nums, target);
        List<List<Integer>> lists1 = fourSum(nums, target);
        for (List<Integer> list : lists) {
            System.out.println(list);
        }
        for (List<Integer> list : lists1) {
            System.out.println(list);
        }
    }
}
